package com.hundsun.yr.universal.p2p.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;

import android.annotation.SuppressLint;
import android.content.Context;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.common.TelephoneUtils;
import com.hundsun.yr.universal.library.data.CheckAndDecodeUtil;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.utils.HSJSONTools;
import com.hundsun.yr.universal.p2p.bean.BaseRequestBean;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.umeng.socialize.utils.Log;


/**
 * @Title: BaseRequestTool.java 
 * @Description: 请求对象工具类
 * @date 2016-1-13 下午3:55:46 
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com 
 * @replace author:
 * @replace date:
 * @version V1.0   
 */
public class BaseRequestTool {
	
	
	/**
	 * 基础Map
	 */
	private  HashMap<String, Object> busiMap = new HashMap<String, Object>();
	
	/**
	 * 子map
	 */
	private  HashMap<String, Object> child = null;
	/**
	 * map缓存
	 */
	private  HashMap<String, HashMap<String, Object>> mapCache = new HashMap<String, HashMap<String, Object>>();
	
	/**
	 * 交易码
	 */
	private  String busiNo; 
	
	
	/**
	 * 设置交易吗
	 * @param no
	 */
	public  void setBusiNo(String no){
		busiNo=no;
	}
	
	
	/**
	 * 获取交易码
	 * @return
	 */
	public  String getBusiNo(){
		return busiNo;
	}
	
	/**
	 * 添加基础map元素
	 * @param key
	 * @param value
	 */
	public  void setBaseMap(String key, Object value) {
		busiMap.put(key, value);
	}
	
	/**
	 * 设置子Map元素
	 * @param parent
	 * @param key
	 * @param value
	 */
	public  void setChildMap(String parentKey, String key, String value) {
		
		if (mapCache.get(parentKey) == null) {
			child = new HashMap<String, Object>();
		} else {
			child = mapCache.get(parentKey);
		}
		child.put(key, value);
		
		mapCache.put(parentKey, child);
		busiMap.put(parentKey, child);
	}
	
	/**
	 * 设置userinfo
	 * @param key
	 * @param value
	 */
	public void setUserInfo(String key, String value){
		setChildMap("userInfo", key, value);
	}
	
	/**
	 * 设置子Map元素(可再加层级)
	 * @param parent
	 * @param key
	 * @param o
	 */
	public  void setChildMap(String parentKey, String key, Object o) {
		
		if (mapCache.get(parentKey) == null) {
			child = new HashMap<String, Object>();
		} else {
			child = mapCache.get(parentKey);
		}
		child.put(key, o);
		
		mapCache.put(parentKey, child);
		busiMap.put(parentKey, child);
	}
	
	
	/**
	 * 获取基础map
	 * @return
	 */
	public  Map<String, Object> getBusiMap() {
		return busiMap;
	}
	
	/**
	 * 赋值基础Map
	 * @param map
	 */
	public  void setBusiMap(HashMap<String, Object> map) {
		busiMap = map;
	}
	
	
	/**
	 * 组装参数
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  HashMap<String, String> AssemblyParams(Context context){
		BaseRequestBean baseRequestBean=new BaseRequestBean() ;
		baseRequestBean.setBusiNo(getBusiNo());
		baseRequestBean.setBusiObject(getBusiMap());
		if(userSP==null){
			userSP = new HSSharedPreferences(context, Config.SPPath.file_userLoginedInfo);
		}
		tokenId=userSP.getShareProf(UserInfo.TOKENID);
		if(userSP.getBoolean(UserInfo.ISLOGIN)&&tokenId!=null)baseRequestBean.setTokenId(tokenId);
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("jsonParam", HSJSONTools.Object2JsonString(baseRequestBean));
		//加密之前的params
		Log.e("params1: " + params + " baseRequestBean: " + baseRequestBean.toString());
//		System.out.println(HSJSONTools.Object2JsonString(baseRequestBean));
		//所有参数加密
		CheckAndDecodeUtil.EncodeAndSign(params, context.getString(R.string.pub_key));
		//加密之后的params参数
		Log.e("params2: " + params);
		return params;
		
	}

	/** 用于读取个人信息*/
	private HSSharedPreferences userSP= null;
	/**
	 * tokenId
	 */
	private  String tokenId; 
	/**
	 * 设置tokenId
	 * @param no
	 */
	public  void setTokenId(String tokenId){
		this.tokenId=tokenId;
	}
	
	/**
	 * 获取tokenId
	 * @return
	 */
	public  String getTokenId(){
		return tokenId;
	}
	/**
	 * sha1签名
	 * @param mContext
	 * @param url
	 * @param user_id
	 * @param key 秘钥
	 * @return
	 */
	@SuppressLint("DefaultLocale") 
	public static String signSHA1(Context mContext,String url,String user_id,String key){
		String signature= "get" + "\n"+
				url+"\n"+
				getFormatedDateString().toLowerCase()+"\n"+
				user_id + "\n" + 
				TelephoneUtils.getIMEI(mContext) + "\n" + 
				"3.0.0" + "\n";
		try {
			return HMACSHA1.HmacSHA1Encrypt(signature, key );
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "";
		}
	}
	/**
	 * post请求
	 */
	public static final String POST="post";
	/**
	 *get请求 
	 */
	public static final String GET="get";
	
	/**
	 * sha1签名
	 * @param mContext
	 * @param url
	 * @param user_id
	 * @param key 秘钥
	 * @return
	 */
	@SuppressLint("DefaultLocale") 
	public static String signSHA1(Context mContext,String methed,String url,String user_id,String key){
		String signature= methed + "\n"+
				url+"\n"+
				getFormatedDateString().toLowerCase()+"\n"+
				user_id + "\n" + 
				TelephoneUtils.getIMEI(mContext) + "\n" + 
				"3.0.0" + "\n";
		
		
		try {
			return HMACSHA1.HmacSHA1Encrypt(signature, key );
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "";
		}
	}
	
	/**
	 * 请求头
	 * @param mContext
	 * @param user_id
	 * @param url
	 * @return
	 */
	public static HashMap<String, String> getHearder(Context mContext,String user_id,String url){
		HSSharedPreferences sharedPreferences = new HSSharedPreferences(mContext, Config.SPPath.file_userLoginedInfo);
		HashMap<String, String> header =new HashMap<String, String>();
		header.put("X-Device-Id", TelephoneUtils.getIMEI(mContext));
		header.put("X-Platform", android.os.Build.VERSION.RELEASE);
		header.put("X-User-Id", user_id);
		header.put("X-Version", "3.0.0");
		header.put("X-ZWGT-DATE", getFormatedDateString());
		header.put("x-auth", signSHA1(mContext, url, user_id,sharedPreferences.getShareProf(UserInfo.CERT_NO)));
		header.put("x-platform-type", 100+"");


		return header;

	}
	
	
	/**
	 * 请求头
	 * @param mContext
	 * @param user_id
	 * @param url
	 * @param key 秘钥key
	 * @param methed 请求方式
	 * @return
	 */
	public static TreeMap<String, String> getHearder(Context mContext,String user_id,String url,String key,String methed){
		TreeMap<String, String> header =new TreeMap<String, String>();
		header.put("X-Device-Id", TelephoneUtils.getIMEI(mContext));
		header.put("X-Platform", android.os.Build.VERSION.RELEASE);
		header.put("X-User-Id", user_id);
		header.put("X-Version", "3.0.0");
		header.put("X-ZWGT-DATE", getFormatedDateString());
		header.put("x-auth", signSHA1(mContext, methed,url, user_id,key));
		header.put("x-platform-type", 100+"");
		
		return header;
		
	}
	
	
	/**
     * 此函数非原创，从网上搜索而来，timeZoneOffset原为int类型，为班加罗尔调整成float类型
     * timeZoneOffset表示时区，如中国一般使用东八区，因此timeZoneOffset就是0
     * @param timeZoneOffset
     * @return
     */
    public static String getFormatedDateString(){
    	TimeZone.setDefault(TimeZone.getTimeZone("GMT+0"));
    	String[] k = (new Date()+"").split(" ");
//    	System.out.println(k[0]+","+k[2]+" "+k[1]+" "+k[5] +" "+k[3]+" +0000");
    	return k[0]+", "+k[2]+" "+k[1]+" "+k[5] +" "+k[3]+" +0000";
    }
	
    
    
	
}
